<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/9/5
 * Time: 10:36
 * 库存管理相关接口
 */
class stock extends CI_Controller
{


    public function __construct()
    {
        parent::__construct();
        $this->load->model('stock_model');
        $this->load->model('stockPut_model');
        $this->load->model('stockOut_model');

        $this->load->model('goods_model');
//        $this->load->model('goodsImage_model');
        $this->load->model('goodsStockPrice_model');
        $this->load->model('purchaseOrder_model');
        $this->load->model('returnOrder_model');

    }

    //库存筛选接口
    public function search(){

        $argc = [];

        //查询一级数据
        if(isset($_GET['oneCategory']) && $_GET['oneCategory']){
            $argc += [
                'one_category' => $_GET['oneCategory']
            ];
        }

        //查询二级
        if(isset($_GET['twoCategory']) && $_GET['twoCategory']){
            $argc += [
                'two_category' => $_GET['twoCategory']
            ];
        }

        //查询三级
        if(isset($_GET['threeCategory']) && $_GET['threeCategory']){
            $argc += [
                'three_category' => $_GET['threeCategory']
            ];
        }

//        echo '<pre>';
//        print_r($argc);
//        die;


        if(isset($_GET['search']) && $_GET['search']){
            $searchArg = [
                'field' => 'name',
                'arg'=> $_GET['search'],
                'side' => 'both'
            ];
        }else{
            $searchArg = '';
        }

        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        if(!$argc){
            $argc = 0;
        }



        if($searchArg){

            $goods = $this->goods_model->search('*',$argc,$searchArg,$page_num,$start_position);

        }else{
            $goods = $this->goods_model->page('goods',$argc,$page_num,$start_position);
        }

        if($goods[0]){
            $ids = array_column($goods[0], 'id');
            foreach ($ids as $v) {
                $arg2 = [
                    'product_id' => $v,
                ];
                $goodsStock[] = $this->goodsStockPrice_model->getByFieldQueryData('id,product_id,specification_group_names,stock,product_stock_priceId', $arg2, 1); //limit查询的数据
            }


            $ckIds = array_column($goods[0], 'ckId');
            foreach ($ckIds as $k8 => $v8) {
                $arg3 = [
                    'product_id' => $v8
                ];

                $goodsCkStock[] = $this->goodsStockPrice_model->getByFieldQueryData('product_id,stock,specification_group_names,product_stock_priceId', $arg3, 1); //limit查询的数据

            }

//            echo '<pre>';
//            print_r($goodsCkStock);
//            die();

            foreach ($goods[0] as $k5=>$v5){
                foreach ($goodsCkStock as $k6=>$v6){
                    foreach ($v6 as $k7=>$v7){

                        if(isset($v7['specification_group_names']) || !empty($v7['specification_group_names'])){
                            if($v7['product_id'] == $v5['ckId']){
                                $goods[0][$k5]['att'] = $v6;

                                $goods[0][$k5]['stock'] = $v7['stock'];
                                $goods[0][$k5]['product_stock_priceId'] = $v7['product_stock_priceId'];

                            }
                        }else{
                            if($v7['product_id'] == $v5['id']){
                                $goods[0][$k5]['att'] = $v6;


                                $goods[0][$k5]['stock'] = $v7['stock'];
                                $goods[0][$k5]['product_stock_priceId'] = $v7['product_stock_priceId'];
                            }
                        }
                    }
                }
            }

            foreach ($goods[0] as $k3 => $v3) {
                foreach ($goodsStock as $k2 => $v2) {

                    foreach ($v2 as $k4=>$v4){
                        if(isset($v4['specification_group_names']) || !empty($v4['specification_group_names'])){
                            if($v4['product_id'] == $v3['id']){
                                $goods[0][$k3]['att'] = $v2;
                            }

                        }else{
                            if($v4['product_id'] == $v3['id']){
                                $goods[0][$k3]['att'] = $v2;
                            }
                        }
                    }



                }
            }


        }else{
            $goods[0] = '';
        }


        $goods['pageNum'] = $page_num;
        $goods['this_page'] = $page;




//        echo '<pre>';
//        print_r($supplier);
//        die;
        echo json_encode($goods,JSON_UNESCAPED_UNICODE);

    }

    //出库管理搜索筛选接口
    public function outSearch(){

        $argc = [];

        if(isset($_GET['startTime'])  && $_GET['startTime']){
            $argc += [
                'createTime >'=> strtotime($_GET['startTime']),
            ];
        }

        if(isset($_GET['endTime'])  && $_GET['endTime']){
            $argc += [
                'createTime <'=> strtotime($_GET['endTime']),
            ];
        }

        //预警状态
        if (isset($_GET['status']) && $_GET['status'] != '' && $_GET['status'] != 'null') {
            if($_GET['status']){
                $argc += [
                    'status ' => $_GET['status']
                ];
            }else{
                $argc += [
                    'status !=' => 1
                ];
            }


        }

        if (isset($_GET['supplier']) && $_GET['supplier'] && $_GET['supplier'] != 'null') {
            $argc += [
                'supplier' => $_GET['supplier'],
            ];
        }

//        echo '<pre>';
//        print_r($argc);
//        die;


        if(isset($_GET['search']) && $_GET['search']){
            $searchArg = [
                'field' => 'name',
                'arg'=> $_GET['search'],
                'side' => 'both'
            ];
        }else{
            $searchArg = '';
        }

        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        if(!$argc){
            $argc = 0;
        }



        if($searchArg){

            $data = $this->stockOut_model->search('*',$argc,$searchArg,$page_num,$start_position);

        }else{
            $data = $this->stockOut_model->page('stockOut',$argc,$page_num,$start_position);

        }



//        foreach ($data[0] as $k=>$v){
//            $data[0][$k]['createTime'] = date('Y-m-d',$v['createTime']);
//
//            unset($data[0][$k]['goodsMessage']);
//        }

        foreach ($data[0] as $k=>$v){
            if($v['returnCode']){
                $arg = [
                    'code' => $v['returnCode']
                ];
                $returnName[] = $this->returnOrder_model->getByFieldQueryData('id,code,name,status',$arg,0);
            }

            $data[0][$k]['createTime'] = date('Y-m-d',$v['createTime']);
//
            unset($data[0][$k]['goodsMessage']);
        }

        foreach ($data[0] as $k2 => $v2){
            if(isset($returnName) && $returnName){
                foreach ($returnName as $v3){
                    if($v2['returnCode']){
                        //判断出库单的code == 关联的code,
                        if($v2['returnCode'] == $v3['code']){
                            $data[0][$k2]['returnName'] = $v3['name'];
                            $data[0][$k2]['returnId'] = $v3['id'];
                            $data[0][$k2]['returnStatus'] = $v3['status'];
                        }
                    }else{
                        $data[0][$k2]['returnName'] = '';
                        $data[0][$k2]['returnId'] = '';
                        $data[0][$k2]['returnStatus'] = '';
                    }
                }
            }else{
                $data[0][$k2]['returnName'] = '';
                $data[0][$k2]['returnId'] = '';
                $data[0][$k2]['returnStatus'] = '';
            }
        }


        $data['pageNum'] = $page_num;
        $data['this_page'] = $page;



//        echo '<pre>';
//        print_r($supplier);
//        die;
        echo json_encode($data,JSON_UNESCAPED_UNICODE);

    }

    //入库管理搜索筛选接口
    public function putSearch(){

        $argc = [];

        if(isset($_GET['startTime'])  && $_GET['startTime']){
            $argc += [
                'createTime >'=> strtotime($_GET['startTime']),
            ];
        }

        if(isset($_GET['endTime'])  && $_GET['endTime']){
            $argc += [
                'createTime <'=> strtotime($_GET['endTime']),
            ];
        }

        //预警状态
        if (isset($_GET['status']) && $_GET['status'] != '' && $_GET['status'] != 'null') {
            if($_GET['status']){
                $argc += [
                    'status ' => $_GET['status']
                ];
            }else{
                $argc += [
                    'status !=' => 1
                ];
            }
        }

        if (isset($_GET['supplier']) && $_GET['supplier'] && $_GET['supplier'] != 'null') {
            $argc += [
                'supplier' => $_GET['supplier'],
            ];
        }

//        echo '<pre>';
//        print_r($argc);
//        die;


        if(isset($_GET['search']) && $_GET['search']){
            $searchArg = [
                'field' => 'name',
                'arg'=> $_GET['search'],
                'side' => 'both'
            ];
        }else{
            $searchArg = '';
        }

        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        if(!$argc){
            $argc = 0;
        }



        if($searchArg){

            $data = $this->stockPut_model->search('*',$argc,$searchArg,$page_num,$start_position);

        }else{
            $data = $this->stockPut_model->page('stockPut',$argc,$page_num,$start_position);

        }

//        foreach ($data[0] as $k=>$v){
//            $data[0][$k]['createTime'] = date('Y-m-d',$v['createTime']);
//
//            unset($data[0][$k]['goodsMessage']);
//        }

        foreach ($data[0] as $k=>$v){
            if($v['purchaseCode']){
                $arg = [
                    'purchaseCode' => $v['purchaseCode']
                ];
                $returnName[] = $this->purchaseOrder_model->getByFieldQueryData('id,purchaseCode,name,status',$arg,0);
            }

            $data[0][$k]['createTime'] = date('Y-m-d',$v['createTime']);
        }

        foreach ($data[0] as $k2 => $v2){
            if(isset($returnName) && $returnName){
                foreach ($returnName as $v3){
                    if($v2['purchaseCode']){
                        //判断出库单的code == purchaseCode,
                        if($v2['purchaseCode'] == $v3['purchaseCode']){
                            $data[0][$k2]['returnName'] = $v3['name'];
                            $data[0][$k2]['returnId'] = $v3['id'];
                            $data[0][$k2]['returnStatus'] = $v3['status'];
                        }
                    }else{
                        $data[0][$k2]['returnName'] = '';
                        $data[0][$k2]['returnId'] = '';
                        $data[0][$k2]['returnStatus'] = '';

                    }
                }
            }else{
                $data[0][$k2]['returnName'] = '';
                $data[0][$k2]['returnId'] = '';
                $data[0][$k2]['returnStatus'] = '';

            }
        }



        $data['pageNum'] = $page_num;
        $data['this_page'] = $page;


//        echo '<pre>';
//        print_r($supplier);
//        die;
        echo json_encode($data,JSON_UNESCAPED_UNICODE);

    }


}










































